<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<c:set var="webroot" value="${pageContext.request.contextPath}" />
<html>
<head>
<meta charset="utf-8">
<title>还款列表</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport"
	content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="format-detection" content="telephone=no">
<link rel="stylesheet" href="${webroot}/static/layui/css/layui.css"
	media="all" />
<link rel="stylesheet" href="${webroot}/static/css/public.css"
	media="all" />
<style type="text/css">
.laytable-cell-checkbox, .laytable-cell-numbers, .laytable-cell-space {
	padding: 6px;
}
.laytable-cell-1-status {
    overflow: inherit;
}
.layui-table-view .layui-table td, .layui-table-view .layui-table th {
    padding: 18px 0;
}
.layui-btn-sm {
    width: 80px;
}
.laytable-cell-1-11 {
    overflow: inherit;
}
.layui-btn+.layui-btn {
    margin-left: 0px;
}
.layui-form-item .layui-input-inline {
    width: 135px;
}
.layui-input-inline {
    width: 45%;
}
.layui-form-label {
   width: 60 !important;
}
.layui-table-cell {
    height: 40 !important;
    overflow: inherit;
}
</style>
</head>
<body class="childrenBody">
	<form class="layui-form">
		<blockquote class="layui-elem-quote quoteBox">
			<form class="layui-form" id="filterForm">
				<input type="hidden" id="roleId" name="roleId">
				<input type="hidden" id="lendId" name="lendId">
				<input type="hidden" id="chanId" name="chanId">
				<input type="hidden" id="staus" name="staus">
				<input type="hidden" id="roleName" value="${user.roleName}">
				<input type="hidden" id="buttType">
				<div class="layui-inline">
					<div class="layui-input-inline" style="width: 70%;">
						<input type="text" class="layui-input userName"
							placeholder="搜索姓名/电话号码" />
					</div>
				</div>
				<div class="layui-inline">
					<label class="layui-form-label">渠道来源:</label>
					<div class="layui-input-inline">
						<select id="channelId" lay-filter="category">
							<option value="">请选择渠道来源</option>
							<c:forEach items="${channelList}" var="l">
								<option value="${l.id}">${l.channelName}</option>
							</c:forEach>
						</select>
					</div>
				</div>
				<div class="layui-inline">
					<label class="layui-form-label">放款平台:</label>
					<div class="layui-input-inline">
						<select id="lendingId" lay-filter="category">
							<option value="">请选择放贷平台</option>
							<c:forEach items="${lendingList}" var="s">
								<option value="${s.id}">${s.lendingName}</option>
							</c:forEach>
						</select>
					</div>
				</div>
				<div class="layui-inline">
					<label class="layui-form-label">状态:</label>
					<div class="layui-input-inline">
						<select id="status" lay-filter="category">
							<option value="">请选择状态</option>
							<option value="1">已结清</option>
							<option value="2">未到期</option>
							<option value="3">逾期</option>
							<option value="4">展期</option>
							<option value="5">售后</option>
						</select>
					</div>
				</div>
				<div class="layui-form-item">
					<div class="layui-inline">
						<label class="layui-form-label">还款日期:</label>
						<div class="layui-input-inline">
							<input type="text" class="layui-input" id="startDate"
								placeholder="开始日期" lay-key="1">
						</div>
						<div class="layui-form-mid">-</div>
						<div class="layui-input-inline">
							<input type="text" class="layui-input" id="endDate"
								placeholder="结束日期" lay-key="2">
						</div>
					</div>
					<div class="layui-inline">
						<label class="layui-form-label">借款日期:</label>
						<div class="layui-input-inline">
							<input type="text" class="layui-input" id="startTime"
								placeholder="开始日期" lay-key="3">
						</div>
						<div class="layui-form-mid">-</div>
						<div class="layui-input-inline">
							<input type="text" class="layui-input" id="endTime"
								placeholder="结束日期" lay-key="4">
						</div>
						<a class="layui-btn search_btn" data-type="reload">查询</a>
						<a class="layui-btn layui-btn-primary reset" data-type="reload">重置</a>
					</div>
				<div class="layui-inline">
					<div class="layui-input-inline">
						<select id="status" lay-filter="buttons">
							<option value="1">全部导出</option>
							<option value="2">批量导出</option>
						</select>
					</div>
				</div>
				</div>
				<!-- <div class="layui-inline">
					<a class="layui-btn layui-btn-normal addNews_btn">全部导出</a> <a
						class="layui-btn layui-btn-normal addNews_btns">批量导出</a>
				</div> -->
			</form>
		</blockquote>
		<table id="newsList" lay-filter="newsList"></table>
	</form>
	<script type="text/javascript" src="${webroot}/static/layui/layui.js"></script>
	<script src="${webroot}/static/js/jquery.min.js" charset="utf-8"></script>
	<script>
		layui
				.use(
						[ 'form', 'layer', 'laydate', 'table', 'laytpl' ],
						function() {
							var form = layui.form, layer = parent.layer === undefined ? layui.layer
									: top.layer, $ = layui.jquery, laydate = layui.laydate, laytpl = layui.laytpl, table = layui.table;

							
							//开始日期
							var insStart = laydate.render({
								elem : '#startDate',
							/* 	min : 0,
								done : function(value, date) {
									//更新结束日期的最小日期
									insEnd.config.min = lay.extend({}, date, {
										month : date.month - 1
									});
								} */
							});

							//结束日期
							var insEnd = laydate.render({
								elem : '#endDate',
						/* 		min : 0,
								done : function(value, date) {
									//更新开始日期的最大日期
									insStart.config.max = lay.extend({}, date,
											{
												month : date.month - 1
											});
								} */
							});

							//开始日期
							var insStarts = laydate.render({
								elem : '#startTime',
						/* 		min : 0,
								done : function(value, date) {
									//更新结束日期的最小日期
									insEnds.config.min = lay.extend({}, date, {
										month : date.month - 1
									});
								} */
							});

							//结束日期
							var insEnds = laydate.render({
								elem : '#endTime',
							/* 	min : 0,
								done : function(value, date) {
									//更新开始日期的最大日期
									insStarts.config.max = lay.extend({}, date,
											{
												month : date.month - 1
											});
								} */
							});

							//新闻列表
							var tableIns = table
									.render({
										elem : '#newsList',
										url : '/admin/repay/querypage',
										cellMinWidth : 95,
										page : true,
										height : "full-125",
										limit : 15,
										limits : [ 10, 15, 20, 25 ,30 , 50, 80, 100],
										id : "newsListTable",
										cols : [ [
												{
													type : "checkbox",
													fixed : "left",
													title : '全选',
													align : "center"
												},
												{
													field : 'userName',
													title : '客户姓名',
													align : "center"
												},
												{
													field : 'mobile',
													title : '客户电话号码',
													align : "center"
												},
												{
													field : 'channelName',
													title : '渠道来源',
													align : 'center'
												},
												{
													field : 'businessMan',
													title : '业务员',
													align : 'center'
												},
												{
													field : 'totalMoney',
													title : '借款金额',
													align : 'center'
												},
												{
													field : 'mailbox',
													title : '还款金额',
													align : 'center',
													templet : function(d) {
														return d.totalMoney+ d.totalAccrual;
													}
												},
												{
													field : 'lendingName',
													title : '放款平台',
													align : 'center'
												},
												{
													field : 'startTime',
													title : '借款时间',
													align : 'center',
													templet : function(d) {
														return layui.laytpl.toDateString(d.startTime,'yyyy-MM-dd');
													}
												},
												{
													field : 'endTime',
													title : '还款日期',
													align : 'center',
													templet : function(d) {
														return layui.laytpl.toDateString(d.endTime,'yyyy-MM-dd');
													}
												},
												{
													field : 'status',
													title : '状态',
													align : 'center',
													templet : function(d) {
														if(d.status == 1){
															return '已结清';
														}else if(d.status == 2){
															return '<select id='+ d.status +' name='+ d.id +' class='+ layui.laytpl.toDateString(d.endTime,'yyyy-MM-dd') +' lay-filter="repays"><option selected="selected" value="">未到期</option> <option value="1">已结清</option> <option value="3">逾期</option><option value="4">展期</option></select>';
														}else if(d.status == 3){
															return '<select id='+ d.status +' name='+ d.id +' class='+ layui.laytpl.toDateString(d.endTime,'yyyy-MM-dd') +' lay-filter="repays"><option selected="selected" value="">逾期</option><option value="1">已结清</option> <option value="4">展期</option><option value="5">售后</option></select>';
														}else if(d.status == 4){
															return '<select id='+ d.status +' name='+ d.id +' class='+ layui.laytpl.toDateString(d.endTime,'yyyy-MM-dd') +' lay-filter="repays"><option selected="selected" value="4">展期</option><option value="1">已结清</option> <option value="3">逾期</option>';
															//return '<a class="layui-btn layui-btn-primary layui-btn-sm" lay-event="zq">展期</a>';
														}else if(d.status == 5){
															return '<select id='+ d.status +' name='+ d.id +' class='+ layui.laytpl.toDateString(d.endTime,'yyyy-MM-dd') +' lay-filter="repays"><option selected="selected" value="5">售后</option><option value="1">已结清</option> <option value="3">逾期</option><option value="4">展期</option></select>';
														}
													}
												},
												{
													field : '',
													title : '操作日志',
													align : 'center',
													templet : function(d) {
													   return '<a class="layui-btn layui-btn-primary layui-btn-sm" lay-event="edit">查看详情</a>';
													}
												},
												{
													title : '备注',
													templet : '#newsListBar',
													fixed : "right",
													align : "center",
													templet : function(d) {
														var str = '';
														if (d.remark == null) {
															str = '<a class="layui-btn layui-btn-xs" lay-event="addBz">添加备注</a>';
														} else {
															str = '<a class="layui-btn layui-btn-xs" lay-event="editBz">编辑备注</a>';
														}
														str += '&nbsp;&nbsp;<a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>';
														return str;
													}
												} ] ]
									});
							 form.on('select(buttons)', function (data) {
								 if(data.value == "1"){
									 layer.confirm('确定到导出吗？', {
											icon : 3,
											title : '提示信息'
										}, function(index) {
											document.location.href="/admin/repay/export";
											layer.close(index);
										});
								 }else{
									 var checkStatus = table.checkStatus('newsListTable'),
							            data = checkStatus.data,
							            id = [];
							            if(data.length > 0) {
							                for (var i in data) {
							                  id.push(data[i].id);
							                }
											layer.confirm('确定到导出吗？', {
												icon : 3,
												title : '提示信息'
											}, function(index) {
												document.location.href="/admin/repay/export?id="+id.join(",");
												layer.close(index);
											})
							            }else{
								            layer.msg("请选择需要导出的数据");
								        }
								 }
							 })

							 form.on('select(category)', function (data) {
								 var id = data.elem.id;
								 if(id == 'channelId'){
									 $("#chanId").val(data.value);
								 }
								 if(id == 'lendingId'){
									 $("#lendId").val(data.value);
								 }
								 if(id == 'status'){
									 $("#staus").val(data.value);
								 }
							});
							 
							 form.on('select(repays)', function (data) {
								 var id = data.value;
								 if(id == 1){
									 upOpens(data.elem.id,data.elem.name,data.value);
									/*  if($("#roleName").val() == "超级管理员"){
										 upOpens(data.elem.id,data.elem.name,data.value);
									 }else{
										 layer.msg("无已结清操作权限");
										 tableIns.reload();
									 } */
								 }else if(id == 2){
									 upStatus(data.elem.name,data.value,'未到期');
									 insertLog(data.elem.name,data.elem.id,data.value);
								 }else if(id == 3){
									 upOpens(data.elem.id,data.elem.name,data.value);
								 }else if(id == 4){
									 upOpen(data.elem.id,data.elem.name,data.value,data.elem.className);
								 }else if(id == 5){
									 upOpens(data.elem.id,data.elem.name,data.value);
								 }
							});
							 
							 function upStatus(id,status,titie){
								 $.post("/admin/repay/update", {
									    id : id,
									    status : status,
									    record : titie
									}, function(data) {
										var res = JSON.parse(data);
										if (res.success) {
											//tableIns.reload();
											layer.close(index);
										} else {
											top.layer.msg(res.message);
										}
								})
							 }
							 
							 function insertLog(id,initStatus,upStatus,titie){
								 $.post("/admin/borrowlog/insert", {
									    id : id,
									    initStatus : initStatus,
									    upStatus : upStatus,
									    record : titie
									}, function(data) {
										var res = JSON.parse(data);
										if (res.success) {
											//tableIns.reload();
											layer.close(index);
										} else {
											top.layer.msg(res.message);
										}
								})
							 }
							 
							 function upOpens(initStatus,id,upStatus){
								   var title ="";
									if(upStatus == "1"){
										title="设置已结清选项";
									}else if(upStatus == "3"){
										title="设置逾期选项";
									}else if(upStatus == "5"){
										title="设置售后选项";
									}
								    var index = layer.open({
							            title : title,
							            type : 2,
							            area : ["500px","280px"],
							            content : "/admin/repay/details",
							            success : function(layero, index){
							                var body = $($(".layui-layer-iframe",parent.document).find("iframe")[0].contentWindow.document.body);
							                if(id){
							                    body.find("#id").val(id);
							                    body.find("#initStatus").val(initStatus);
							                    body.find("#upStatus").val(upStatus);
							                    form.render();
							                }
							            },
							            end:function(){
							            	if($("#buttType").val() != 'edit'){
							            		//tableIns.reload();
							            	}
							            }
							        })
							 }
							 
							 function upOpen(initStatus,id,upStatus,endTime){
								   var title =""; var url ="";
								   var wid="";  var hig="";
									if(upStatus){
										title="设置展期选项";
										url="/admin/repay/showpost?endTime="+endTime;
										wid="500px";
										hig="420px";
									}else{
										title="操作日志";
										url="/admin/borrowlog/showlist";
										wid="550px";
										hig="500px";
									}
								    var index = layer.open({
							            title : title,
							            type : 2,
							            area : [wid,hig],
							            content : url,
							            success : function(layero, index){
							                var body = $($(".layui-layer-iframe",parent.document).find("iframe")[0].contentWindow.document.body);
							                if(id){
							                    body.find("#id").val(id);
							                    body.find("#initStatus").val(initStatus);
							                    body.find("#upStatus").val(upStatus);
							                    //body.find("#endTime").val(endTime);
							                    form.render();
							                }
							            },
							            end:function(){
							            	if($("#buttType").val() != 'edit'){
							            		//tableIns.reload();
							            	}
							            }
							        })
							 }
							 
							//是否置顶
							form.on('switch(newsTop)', function(data) {
								var index = layer.msg('修改中，请稍候', {
									icon : 16,
									time : false,
									shade : 0.8
								});
								setTimeout(function() {
									layer.close(index);
									if (data.elem.checked) {
										layer.msg("置顶成功！");
									} else {
										layer.msg("取消置顶成功！");
									}
								}, 500);
							})

							form.on('select', function(data) {
								$("#roleId").val(data.value); //得到被选中的值]
							});

							//搜索【此功能需要后台配合，所以暂时没有动态效果演示】
							$(".search_btn").on("click", function() {
								table.reload("newsListTable", {
									page : {
										curr : 1
									//重新从第 1 页开始
									},
									where : {
										key : $(".userName").val(),
										startDate : $("#startTime").val(),
										endDate : $("#endTime").val(),
										startTime : $("#startDate").val(),
										endTime : $("#endDate").val(),
										channelId :  $("#chanId").val(),
										lendingId : $("#lendId").val(),
										status : $("#staus").val()
									}
								});
							});
							
							//重置
							$(".reset").on("click", function() {
								document.location.href="/admin/repay/showlist"
								/* $(".userName").val("");
								$("#startDate").val("");
								$("#endDate").val("");
								$("#startTime").val("");
								$("#endTime").val("");
								$("#chanId").val("");
								$("#lendId").val("");
								$("#staus").val("");
								$("#channelId").val("");
								$("#lendingId").val("");
								$("#status").val("");
								form.render('select');
								table.reload("newsListTable", {
									page : {
										curr : 1
									//重新从第 1 页开始
									},
									where : {
										key : '',
										startDate : '',
										endDate : '',
										startTime : '',
										endTime : '',
										channelId :  '',
										lendingId : '',
										status : ''
									}
								}); */
							});
/* 
							$(".addNews_btn").click(function() {
									layer.confirm('确定到导出吗？', {
										icon : 3,
										title : '提示信息'
									}, function(index) {
										document.location.href="/admin/repay/export";
										layer.close(index);
									});
							}) 
							$(".addNews_btns").click(function() {
								  var checkStatus = table.checkStatus('newsListTable'),
						            data = checkStatus.data,
						            id = [];
						            if(data.length > 0) {
						                for (var i in data) {
						                  id.push(data[i].id);
						                }
										layer.confirm('确定到导出吗？', {
											icon : 3,
											title : '提示信息'
										}, function(index) {
											document.location.href="/admin/repay/export?id="+id.join(",");
											layer.close(index);
										})
						            }else{
							            layer.msg("请选择需要导出的数据");
							        }
							})  */

							//列表操作
							table.on('tool(newsList)', function(obj) {
								var layEvent = obj.event, data = obj.data;
							    if(layEvent === 'edit'){
							    	upOpen("",data.id,"","");
							    	$("#buttType").val('edit');
							    	//window.location.href="/admin/borrowlog/showlist?id="+data.id;
								}else if(layEvent === 'editBz'){
									addNews(data);
								}else if(layEvent === 'addBz'){
									addNews(data);
								}else if(layEvent === 'del'){
									if($("#roleName").val() != "超级管理员"){
										return layer.msg("无删除操作权限");
									}
									layer.confirm('确定删除此还款用户吗？',{icon:3, title:'提示信息'},function(index){
						                $.get("/admin/repay/delete",{
						                     id : data.id,
						                     userName : data.userName,
						                     record : '删除还款用户:'
						                },function(data){
						                  var res=JSON.parse(data);
						               	  if(res.success){
						               		layer.msg("删除成功");
						               	    // tableIns.reload();
						               	    setTimeout(function(){layer.close(index);},2000);
						             	  }else{
						             		 top.layer.msg(res.message);
						             	  }
						                })
						            });
								}
							});
							
							//添加成员
							function addNews(edit) {
								var title =""; var url ="";
								if(edit){
									title="编辑备注";
									url="/admin/borrow/addremark?id="+edit.id;
								}else{
									title="添加备注";
									url="/admin/borrow/addremark";
								}
							    var index = layer.open({
						            title : title,
						            type : 2,
						            area : ["500px","355px"],
						            content : url,
						            success : function(layero, index){
						                var body = $($(".layui-layer-iframe",parent.document).find("iframe")[0].contentWindow.document.body);
						                if(edit){
						                    body.find(".remark").val(edit.remark);
						                    body.find("#id").val(edit.id);
						                    body.find("#mark").val(edit.remark);
						                    form.render();
						                }
						            },
						            end:function(){
						            	//tableIns.reload();
						            }
						        })
							}

							// 时间戳的处理
							layui.laytpl.toDateString = function(d, format) {
								var date = new Date(d || new Date()), ymd = [
										this.digit(date.getFullYear(), 4),
										this.digit(date.getMonth() + 1),
										this.digit(date.getDate()) ], hms = [
										this.digit(date.getHours()),
										this.digit(date.getMinutes()),
										this.digit(date.getSeconds()) ];

								return format.replace(/yyyy/g, ymd[0]).replace(
										/MM/g, ymd[1]).replace(/dd/g, ymd[2])
										.replace(/HH/g, hms[0]).replace(/mm/g,
												hms[1]).replace(/ss/g, hms[2]);
							};
							// 数字前置补零
							layui.laytpl.digit = function(num, length, end) {
								var str = '';
								num = String(num);
								length = length || 2;
								for (var i = num.length; i < length; i++) {
									str += '0';
								}
								return num < Math.pow(10, length) ? str
										+ (num | 0) : num;
							};
						});
	</script>
</body>
</html>